package middleware

import (
	"github.com/gin-contrib/sessions"
	"github.com/gin-contrib/sessions/cookie"
	"github.com/gin-gonic/gin"
)

// Session 会话中间件
func Session() gin.HandlerFunc {
	// 使用cookie存储会话数据
	store := cookie.NewStore([]byte("tolog_secret_key"))
	
	// 配置会话
	store.Options(sessions.Options{
		Path:     "/",
		MaxAge:   86400 * 7, // 7天
		HttpOnly: true,
	})
	
	return sessions.Sessions("tolog_session", store)
}

// CurrentUser 获取当前登录用户中间件
func CurrentUser() gin.HandlerFunc {
	return func(c *gin.Context) {
		// 获取会话
		session := sessions.Default(c)
		
		// 获取用户ID
		userID := session.Get("user_id")
		if userID != nil {
			c.Set("user_id", userID)
		}
		
		c.Next()
	}
}